<?php
/**
*	作者：信呼开发团队(雨中磐石)
*	官网：http://www.rockoa.com
*	软件：信呼云平台
*	部门
*/
class deptClassModel extends Model
{
	public $alldarr = array();
	public $daldarr = array();
	
	/**
	*	获取所有部门上级id
	*/
	public function getalldid($cid, $did)
	{
		$key = 'd'.$cid.''.$did.'';
		if(isset($this->alldarr[$key]))return $this->alldarr[$key];
		$this->deptallid = ''.$did.'';
		$this->getalldidss($cid, $did);
		$this->alldarr[$key] = $this->deptallid;
		return $this->deptallid;
	}
	private function getalldidss($cid, $did)
	{
		if($did>0){
			$rs = $this->getone('`id`='.$did.'');
			if($rs && $rs['pid']>0){
				$this->deptallid.=','.$rs['pid'].'';
				$this->getalldidss($cid, $rs['pid']);
			}
		}
	}
	
	
	//获取所有部门上级id，包含全部，返回数组
	public function getalldids($ars)
	{
		$deptid  = arrvalue($ars, 'deptid');
		$deptids = arrvalue($ars, 'deptids');
		if(!$deptid)return '';
		$cid  = 0;
		$dida = array();
		if(!isempt($deptids))$deptid.=','.$deptids.'';
		$detpa= explode(',', $deptid);
		foreach($detpa as $s){
			$dids = $this->getalldid($cid, $s);
			if($dids){
				$did2 = explode(',', $dids);
				foreach($did2 as $s1)if(!in_array($s1, $dida))$dida[] = $s1;
			}
		}
		return $dida;
	}
	
	//获取所有部门下级
	public function getdownid($cid, $did)
	{
		if(!$did)return '';
		$key = 'd'.$cid.''.$did.'';
		if(isset($this->daldarr[$key]))return $this->daldarr[$key];
		$this->deptallid = ''.$did.'';
		$this->getdownidss($cid, $did);
		$this->daldarr[$key] = $this->deptallid;
		return $this->deptallid;
	}
	private function getdownidss($cid, $did)
	{
		$rows = $this->getall('`pid`='.$did.'');
		if($rows)foreach($rows as $k=>$rs){
			$this->deptallid.=','.$rs['id'].'';
			$this->getdownidss($cid, $rs['id']);
		}
	}
	
	
	/**
	*	$glx=0默认，1通讯录
	*/
	public function getData($uid,$glx=0)
	{
		$deptarr 	= $this->getDept();
		$useraarr 	= m('users')->getall('`status`=1','id,name,ranking,deptname,deptid,face','sort desc');
		
		return array(
			'deptarr' 	=> $deptarr,
			'useraarr' 	=> $useraarr,
		);
	}
	
	public function getDept()
	{
		$this->drows	= array();
		$rows 	= $this->getall('1=1','*','`sort` desc');
		$this->getdepts($rows, 0, 1);
		return $this->drows;
	}
	
	private function getdepts($rows, $pid, $oi)
	{
		$len = 0;
		foreach($rows as $k=>$rs){
			if($rs['pid']==$pid){
				$rs['level'] = $oi;
				$this->drows[] = $rs;
				$cdus = count($this->drows)-1;
				$len++;
				$cdu = $this->getdepts($rows,$rs['id'], $oi+1);
				$this->drows[$cdus]['stotal'] = $cdu;
			}
		}
		return $len;
	}
	
	//读取receid下相关人员
	public function getjoinwhere($receid, $ars)
	{
		if(isempt($receid))return '';
		$uarr = $darr = $garr = array();
		$cid  = $ars['cid'];
		$recea= explode(',', $receid);
		foreach($recea as $st1){
			$st2 = str_replace(array('u','d','g'),'', $st1);
			if(contain($st1,'d')){
				$dids 	= $this->getdownid($cid, $st2);
				$darr[] = $st2;
				if($dids){
					$didsa = explode(',', $dids);
					foreach($didsa as $dis)if(!in_array($dis, $darr))$darr[] = $dis;
				}
			}else if(contain($st1,'g')){
				$garr[] = $st2;
			}else{
				$uarr[] = $st2;
			}
		}
		$whera = array();
		if($uarr)$whera[] = '`id` in('.join(',', $uarr).')';
		if($darr){
			$whera[] = '`deptid` in('.join(',', $darr).')';
			foreach($darr as $did){
				//$whera[] = $this->rock->dbinstr('`deptids`', $did);
			}
		}
		if(!$whera)return '';
		$wher1 = ' and ('.join(' or ', $whera).')';
		if(in_array(1, $darr))$wher1='';
		$where = '`status`=1'.$wher1.'';
		return $where;
	}
	
	//条件替换
	public function replacewhere($ars,$str, $fobj=null)
	{
		if(!contain($str,'{'))return $str;
		$barr 	= $this->rock->matcharr($str);
		$deptid  = arrvalue($ars,'deptid');
		$deptids = arrvalue($ars,'deptids');
		$cid 	= 0;
		$myar 	= array();
		foreach($barr as $match){
			$rstr = $type = '';
			$_artr= explode(',', $match);
			$fid	= $_artr[0];
			$type	= $fid;
			if(isset($_artr[1]))$type = $_artr[1];
			$tnes	= arrvalue($_artr,2);
			//{receid}
			if($type=='receid'){
				$tjarr = array();
				if(!$myar){
					$myar = array('u'.$ars['id'].'');
					$dida = $this->getalldids($ars);
					if($dida)foreach($dida as $tes)$myar[] = 'd'.$tes.'';
				}
				if($tnes!='not')$tjarr[] = '`'.$fid.'` is null';
				foreach($myar as $kv1)$tjarr[] = $this->rock->dbinstr($fid, $kv1);
				$rstr = '('.join(' or ', $tjarr).')';
			}
			//{date}
			if($type=='date'){
				$rstr= $this->rock->date;
			}
			//{unread}
			if($type=='unread'){
				$rstr = '`id` not in(select `mid` from `[Q]flowread` where `cid`='.$cid.' and `aid`='.$ars['id'].' and `table`=\''.$fobj->mtable.'\')';
			}
			if($rstr)$str = str_replace('{'.$match.'}', $rstr, $str);
		}
		return $str;
	}
}